import request from '@/utils/request'

// 用户相关接口类型定义
export interface LoginParams {
  username: string
  password: string
}

export interface LoginResponse {
  token: string
  userInfo: any
}

export interface User {
  id: number
  username: string
  realName: string
  email: string
  phone: string
  avatar?: string
  status: number
  lastLoginTime?: string
  lastLoginIp?: string
  loginCount: number
  createTime: string
  updateTime: string
  roles?: Role[]
}

export interface Role {
  id: number
  roleName: string
  roleCode: string
  description?: string
  status: number
}

export interface UserCreateDTO {
  username: string
  password: string
  realName: string
  email?: string
  phone?: string
  avatar?: string
  status?: number
  roleIds?: number[]
}

export interface UserUpdateDTO {
  realName?: string
  email?: string
  phone?: string
  avatar?: string
  status?: number
  roleIds?: number[]
}

export interface UserQueryDTO {
  pageNum?: number
  pageSize?: number
  username?: string
  realName?: string
  email?: string
  phone?: string
  status?: number
}

export interface PageResult<T> {
  records: T[]
  total: number
  size: number
  current: number
  pages: number
}

// 认证相关API
export const loginApi = (username: string, password: string) => {
  return request.post<LoginResponse>('/auth/login', {
    username,
    password
  })
}

export const getUserInfoApi = () => {
  return request.get('/auth/userInfo')
}

export const logoutApi = () => {
  return request.post('/auth/logout')
}

// 用户管理API
export const getUserListApi = (params: UserQueryDTO) => {
  return request.get<PageResult<User>>('/v1/users', { params })
}

export const getUserByIdApi = (id: number) => {
  return request.get<User>(`/v1/users/${id}`)
}

export const createUserApi = (data: UserCreateDTO) => {
  return request.post<number>('/v1/users', data)
}

export const updateUserApi = (id: number, data: UserUpdateDTO) => {
  return request.put<boolean>(`/v1/users/${id}`, data)
}

export const deleteUserApi = (id: number) => {
  return request.delete<boolean>(`/v1/users/${id}`)
}

export const resetPasswordApi = (id: number, newPassword: string) => {
  return request.post<boolean>(`/v1/users/${id}/reset-password`, {
    newPassword
  })
}

export const checkUsernameApi = (username: string) => {
  return request.get<boolean>('/v1/users/check-username', {
    params: { username }
  })
}

export const checkEmailApi = (email: string) => {
  return request.get<boolean>('/v1/users/check-email', {
    params: { email }
  })
}

export const checkPhoneApi = (phone: string) => {
  return request.get<boolean>('/v1/users/check-phone', {
    params: { phone }
  })
}
